using UnityEngine;
using System.Collections;

public class axe : MonoBehaviour 
{
    private bool onMovement;
    private Vector3 target;
    private Transform myTransform;

    private const float NEAR = 0.5f;
    private const float ROTATION_SPEED = 20.0f;
    private const float MOVE_SPEED = 20.0f;

	void Start () 
    {
        onMovement = false;
        myTransform = transform;
	}

	// Update is called once per frame
	void Update () 
    {
        if (!onMovement)
            return;

        if (distanceToTarget() <= NEAR)
        {
            onMovement = false;
            return;
        }

        moveToTarget();
	}

    /// <summary>
    /// Advances to the target
    /// </summary>
    private void moveToTarget()
    {
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
                            Quaternion.LookRotation(target - myTransform.position),
                            ROTATION_SPEED * Time.deltaTime);

        myTransform.position += (myTransform.forward * MOVE_SPEED * Time.deltaTime);
    }

    /// <summary>
    /// Calculates the distance 
    /// to the target
    /// </summary>
    /// <returns>The distance</returns>
    private float distanceToTarget()
    {
        return Vector3.Distance(myTransform.position, target);
    }

    /// <summary>
    /// Starts moving the axe to the given position
    /// </summary>
    /// <param name="position">The target of the axe</param>
    public void moveTo(Vector3 position)
    {
        target = position;
        onMovement = true;
    }

    /// <summary>
    /// Returns if the axe is moving or not
    /// </summary>
    /// <returns>Axe moving</returns>
    public bool isMoving()
    {
        return onMovement;
    }
}
